<template>
	<view class="content">
		<view class="myads_top">
			<view class="myads_top_L" @tap="to_add">
				<text class="iconfont icon-54"></text>
				<text>新增地址</text>
			</view>
			<text @tap="edittap">{{chose?'完成':'管理'}}</text>
		</view>
		<view class="myads_content">
			
			<view class="myads_content_li" v-for="(item,index) in adslist" :key='index'>
				<text class="iconfont icon-duihao2" :style="item.select_del?'color: #5a70fb;':'color:#e5e5e5'" v-if="chose" @tap='select_del(index)'></text>
				<view :class="currentItemId == index?'active myads_content_li_M':'myads_content_li_M' " @tap="chosetap(index)">
					<view class="li_M_name">
						<text>{{item.contact}}</text>
						<text class="phone">{{item.phone}}</text>
						<text class="icon" v-if="item.is_default==1">默认</text>
					</view>
					<view class="li_M_ads">
						<text>{{item.area}}</text>
						<text>{{item.street}} {{item.address}}</text>
					</view>
				</view>
				<text class="iconfont icon-wentifankui1" @tap="to_edit(item.id)" v-if="!is_delete_model"></text>
			</view>
			
			
		</view>
		<button @tap='dalete' v-if="is_delete_model" >删除</button>
		<toast></toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				chose: false,
				select: false, //是否开启选择功能
				is_delete_model:false, //是否删除
				currentItemId:1,
				adslist:[
				],
				del_ids:[],
			};
		},
		methods:{
			// 勾选
			select_del(index){
				console.log(index)
				this.adslist[index].select_del = !this.adslist[index].select_del
				
			},
			// 管理
			edittap(){ 
				this.is_delete_model = !this.is_delete_model;
				this.chose = !this.chose;
			},
			// 选择地址
			chosetap(index){
				console.log(index)
				if(this.is_delete_model){
					this.select_del(index)
				}else{
					this.$alert("选择当前地址")
				}
				// this.currentItemId = index;
				 
			},	
			// 删除
			dalete(){
				let ids = [];
				this.adslist.forEach(item=>{
					// console.log(item);
					if(item.select_del){
						ids.push(item.id)
					}
				});
				uni.showModal({
					title:"温馨提示",
					content:"确定删除选中的地址吗",
					success:e=>{
						if(e.confirm){
							this.$ajax.post({
								url:"/Address/deleteAddress",
								data:{
									address_id:ids.toString()
								}
							}).then(res=>{
								this.$alert(res.msg);
								if(res.code==200){
									this.get_my_address()
								}
							})
						}
					}
				})
				
				
			},
			// 编辑
			to_edit(id){
				uni.navigateTo({
					url: '/pages/address_manager/edit?id='+id
				})
			},
			// 添加
			to_add(){
				uni.navigateTo({
					url: '/pages/address_manager/add'
				})
			},
			// 获取我的地址列表
			get_my_address(){
				this.$ajax.post({url:'/Address/index'}).then(res=>{
					console.log(res);
					if(res.code==200){
						for(let i=0;i<res.data.length;i++){
							res.data[i].select_del = false
						}
						this.adslist = res.data;
					}else{
						this.$alert(res.msg)
					}
				})
			}
		},
		onShow() {
			this.get_my_address()
		},
		onLoad(opt) {
			//判断是否从release页面进入，如果是则开启选择功能
			if(opt.select){
				this.select = true
			}
			
		}
	}
</script>

<style>
	.content{
		box-shadow:0 8px 10px -8px #eee inset;
		border-top: 1px solid #eee;
	}
	
.myads_top{
	display: flex;
	justify-content: space-between;
	line-height: 100upx;
	margin: 0 3%;
	width: 94%;
	box-sizing: border-box;
	overflow: hidden;
	border-bottom: 1px solid #eeeeee;
}
.myads_top_L{
	color: #596FFE;
	width: 590upx;
	float: left;
	display:inline-block;
}
.myads_top_L .iconfont{
	font-size: 34upx;
	font-weight: 600;
	margin-right: 15upx;
}
.myads_top_L .iconfont{
	font-size: 36upx;
}
.li_M_name .icon{
	color: #5a70fb;
	border: 1px solid #5a70fb;
	font-size: 22upx;
	padding: 0 15upx;
	border-radius: 50upx;
	display: inline-block;
	line-height: 30upx;
	height: 30upx;
}
.li_M_ads{
	color: #a4a8b4;
	font-size: 24upx;
}
.myads_content{
	/* padding: 0 20upx; */
	box-sizing: border-box;
}
.li_M_name{
	padding-bottom: 20upx;
}
.li_M_name .phone{
	padding: 0 20upx;
}
.myads_content_li{
	display: flex;
	border-bottom: 1px solid #eeeeee;
	align-items: center;
	padding: 40upx 20upx;
	box-sizing: border-box;
}
.li_M_ads text{
	display:block ;
}
.li_M_ads text::last-child{
	padding-bottom: 30upx;
}
.myads_content_li_M{
	padding: 0 20upx;
	box-sizing: border-box;
	width: 600upx;
}
.icon-duihao2{
	font-size: 44upx;
}
.icon-wentifankui1{
	color: #d9d9d9;
	font-size: 54upx;
}
button{
	background: #ff5d76;
	color: #fff;
	border-radius: 50upx;
	width: 250upx;
	line-height: 70upx;
	height: 70upx;
	margin-top: 100upx;
	font-size: 34upx;
}
button:after{
	border: none;
}
</style>
